--拙山尽起
local zhuoshanjinqi = fk.CreateSkill {
    name = "mcdh_zhuoshanjinqi",
    tags = {Skill.Compulsory}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

zhuoshanjinqi:addEffect("invalidity",{
    invalidity_func = function(self, from, skill)
        return from:hasSkill(self, false, true) and skill.name~= zhuoshanjinqi.name
                and not (skill:isEquipmentSkill() or skill.name:endsWith("&"))
    end
})


zhuoshanjinqi:addEffect(fk.Damage,{
    can_trigger = function(self, event, target, player, data)
        return target==player and player:hasSkill(zhuoshanjinqi.name)  and #player.room.logic:getActualDamageEvents(2, function(e)
        return e.data.from == target or e.data.to == target
      end) == 1
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local choices = {"jinqi1","jinqi2", "jinqi-offense"}
        local can_throw = table.filter(player:getCardIds("he"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
        if #can_throw == 0 and player == data.from then
            table.removeOne(choices,"jinqi1" )
        end

        if  not data.from or data.from:isKongcheng() then
            table.removeOne(choices,"jinqi1" )
        end

        local targets = table.map(table.filter(room.alive_players, function(p)
            return #p:getCardIds("eh") > 0 and p ~= data.to end), Util.IdMapper)
        if #targets == 0 then
            table.removeOne(choices,"jinqi2" )
        end
        local choice = room:askForChoice(player, choices, self.name, nil, nil,{"jinqi1","jinqi2", "jinqi-offense"})
        event:setCostData(self,choice)
        return true
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)

        local banner = room:getBanner(zhuoshanjinqi.name) or {}
        local index = 1
        if #banner > 0 then
            index = banner[#banner][1][1] + 1
        end
        local extra_data = {}
        extra_data.zhuoshanjinqi = index
        table.insert(banner, {index, {}})
        room:setBanner(zhuoshanjinqi.name, banner)

        if choice == "jinqi1" or choice == "jinqi-offense" then
            if data.from ~= player then
                local  cards = room:askForCardsChosen(player, data.from,1, 1, "he", self.name, "#jinqi1:"..data.from.id)
                lib.moveCardsWithExtraData(room,extra_data,{
                    ids = cards,
                    from = data.from,
                    toArea = Card.DiscardPile,
                    moveReason = fk.ReasonDiscard,
                    proposer = player,
                    skillName = zhuoshanjinqi.name
                })
            else
                local cards =room:askToDiscard(player,{skill_name = zhuoshanjinqi.name,min_num=1,max_num=1,cancelable=false,skip=true})
                lib.moveCardsWithExtraData(room,extra_data,{
                    ids = cards,
                    from = data.from,
                    toArea = Card.DiscardPile,
                    moveReason = fk.ReasonDiscard,
                    proposer = player,
                    skillName = zhuoshanjinqi.name
                })
            end
        end
        if choice == "jinqi2" or choice == "jinqi-offense" then
            local targets2 = table.filter(room.alive_players, function(p)
                return #p:getCardIds("eh") > 0 and p ~= data.to end)
            if #targets2>0 then
                local to2 = room:askToChoosePlayers(player,{targets=targets2,min_num=1,max_num=1,cancelable=false,skill_name = zhuoshanjinqi.name,prompt = "#jinqi2:"..data.to.id})
                local cid = room:askToCards(to2[1],{min_num=1,max_num=2,skill_name=zhuoshanjinqi.name,cancelable=false,include_equip=true})
               lib.moveCardsWithExtraData(room,extra_data,{
                    ids = cid,
                    from = to2[1],
                    to = data.to,
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonGive,
                    skillName = zhuoshanjinqi.name,
                })
            end

            if choice == "jinqi-offense" then
                room:loseHp(player,1,self.name)
                room:handleAddLoseSkills(player,  "-mcdh_zhuoshanjinqi", nil, true)
            end
        end

        local targets = {}
        banner = room:getBanner(zhuoshanjinqi.name) or {}
        for i, info in ipairs(banner) do
                if info[1] == index then
                    targets = table.map(info[2], Util.Id2PlayerMapper)
                     table.remove(banner, i)
                end
        end
        if not player.dead then
            player:drawCards(1,zhuoshanjinqi.name)
        end
        
        if #targets > 0 then
            room:sortByAction(targets)
            for _, p in pairs(targets) do
                if not p.dead then
                     p:drawCards(1,zhuoshanjinqi.name)
                end
            end
        end
        
    end,
})

zhuoshanjinqi:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        return player.room:getBanner(zhuoshanjinqi.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local tos = {}
        local banner = room:getBanner(zhuoshanjinqi.name)
        for _, move in ipairs(data) do
            if move.extra_data and move.extra_data.zhuoshanjinqi and move.from and move.to ~= move.from  and
                    table.find(move.moveInfo, function (info)
                        return Fk:getCardById(info.cardId).color == Card.Black and
                                (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) 
                    end) then
                         for _, info in ipairs(banner) do
                            if info[1] == move.extra_data.zhuoshanjinqi then
                                table.insertIfNeed(info[2],move.from.id)
                            end
                        end
            end
        end
         room:setBanner(zhuoshanjinqi.name, banner)
    end,
})
Fk:loadTranslationTable{
    ["mcdh_zhuoshanjinqi"]="尽起",
    [":mcdh_zhuoshanjinqi"] = "『拙山尽起』<br>锁定技，你的其他技能失效。当你每回合首次造成或受到伤害后，你选择一项：1.弃置伤害来源一张牌；2.令一名角色交给受伤角色一至两张牌；背水：失去1点体力与本技能。然后你摸一张牌，因此失去黑色牌的角色各摸一张牌。",
    ["jinqi1"]="1.弃置伤害来源一张牌;",
    ["jinqi2"]="2.令一名角色交给受伤角色一至两张牌",
    ["#jinqi1"] = "尽起：弃置 %src 一张牌",
    ["#jinqi2"] = "尽起：选择一名角色，令其交给 %src 一至两张牌",
    ["jinqi-offense"]="背水:失去1点体力和本技能。",

}

return zhuoshanjinqi